package cn.rick.controller;

import cn.dev33.satoken.util.SaResult;
import cn.rick.entity.SysLogininfor;
import cn.rick.service.ISysLogininforService;
import cn.rick.util.EasyExcelUtil;
import cn.rick.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Rick
 * @date 2022-10-27 14:38
 */
@Api(value = "登录日志控制器", tags = {"登录日志控制器"})
@RestController
@RequestMapping("/api/logininfor")
public class LoginInfoController {

    @Resource
    private ISysLogininforService iSysLogininforService;

    @GetMapping("/getData")
    @ApiOperation("获取登录日志信息")
    public SaResult getData(SysLogininfor sysLogininfor, String startDate, String endDate, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<SysLogininfor> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(sysLogininfor.getLoginLocation())) {
            queryWrapper.like(SysLogininfor::getLoginLocation, sysLogininfor.getLoginLocation());
        }
        if (StringUtils.isNotEmpty(sysLogininfor.getUserName())) {
            queryWrapper.like(SysLogininfor::getUserName, sysLogininfor.getUserName());
        }
        if (StringUtils.isNotEmpty(sysLogininfor.getStatus())) {
            queryWrapper.eq(SysLogininfor::getStatus, sysLogininfor.getStatus());
        }
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.between(SysLogininfor::getLoginTime, startDate, endDate + " 23:59:59");
        }

        queryWrapper.orderByDesc(SysLogininfor::getLoginTime);

        PageHelper.startPage(pageNum, pageSize);
        List<SysLogininfor> list = iSysLogininforService.list(queryWrapper);
        PageInfo<SysLogininfor> pageInfo = new PageInfo<>(list);

        return SaResult.data(pageInfo);
    }

    @GetMapping("/download")
    @ApiOperation("下载登录日志信息")
    public void download(SysLogininfor sysLogininfor, String startDate, String endDate, HttpServletResponse response) {
        LambdaQueryWrapper<SysLogininfor> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(sysLogininfor.getLoginLocation())) {
            queryWrapper.like(SysLogininfor::getLoginLocation, sysLogininfor.getLoginLocation());
        }
        if (StringUtils.isNotEmpty(sysLogininfor.getUserName())) {
            queryWrapper.like(SysLogininfor::getUserName, sysLogininfor.getUserName());
        }
        if (StringUtils.isNotEmpty(sysLogininfor.getStatus())) {
            queryWrapper.eq(SysLogininfor::getStatus, sysLogininfor.getStatus());
        }
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.between(SysLogininfor::getLoginTime, startDate, endDate + " 23:59:59");
        }

        queryWrapper.orderByDesc(SysLogininfor::getLoginTime);
        List<SysLogininfor> list = iSysLogininforService.list(queryWrapper);


        EasyExcelUtil.writeExcel(response, list, SysLogininfor.class, "登录日志信息");
    }

}
